<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
 * this data processor provides the base for dealing with multiple files
 * related to a parent entry in a one-to-many fashion (i.e. one parent may
 * have many associated files).
 * 
 * data processors which inherit from this one are responsible for providing
 * a valid relational model.
 */
require_once('DataProcessor.php');

class ProcessSingleItem extends DataProcessor{
	var $model = null;
	
	function ProcessSingleItem( &$p ){
		parent::DataProcessor($p);
	}
	
	function _setupModel(&$params){
		if( isset($params['table']) ){
			$this->model->setTable($params['table']);
		}
		if( isset($params['relational_fields'])){
			$this->model->setRelationalFields($params['relational_fields'][0], $params['relational_fields'][1]);
		}
	}
	
	/* removes relational entries, and item entries */
	function removeAllItems($id, $fields){
		if( $id ){
			$item_ids = array();
			foreach( $fields as $key => $value){
				/*
				 * grab the item ids and delete the entries from the relational table
				 */
				$this->_setupModel($value);
				$item_ids = array_merge( $item_ids, $this->model->deleteRelatedItems($id));
			}
			// delete the entries from the items table
			$this->model->deleteItems($item_ids);
		}
	}
	
	function saveNewItem($id, $fields){
		foreach( $fields as $key => $value){
			//echo "saveNewItem: $key<br />";
			//echo_html_object($value);
			//echo "<br /><br />";
			if( $this->ci->db->table_exists($value['table'])){
				$this->_setupModel($value);
				/* grab new images */
				$temp = $this->ci->input->post($value['new_field']);
				$delete = $this->ci->input->post('delete_' . $key);
				if( $temp || $delete){
					/* if this is an edit remove the previous related entry */
					if( !empty($id) ){
						//echo "this is an edit: $id, deleting related items from $key";
						$delete_ids = $this->model->deleteRelatedItems( $id );
						//echo "deleting ids:";
						//echo_html_object($delete_ids);
						//echo "before deleteItems...<br />";
						$this->model->deleteItems($delete_ids);
						//echo "after deleteItems...<br />";
					}
					if( $temp ){
						//echo 'before parseOptionString<br />';
						$temp = array_keys($this->_parseOptionString($temp));
						//echo 'before parseOptionString<br />';
					
						/* if delete is not checked */
						if( !$this->ci->input->post( 'delete_' . $key) ){
							if( count($temp) ){
								$to_add = array_pop($temp);
								//echo "adding latest item to relational database: $to_add<br />";
								$this->model->addRelatedItems( $id, $to_add);
							}
						}
						/* delete the other uploaded images */
						//echo "deleting rest of items: ";
						//echo_html_object($temp);
						$this->model->deleteItems($temp);
						unset($temp);
					}
				}
			}else{
				show_error("ProcessSingleItem::saveItem - table $key does not exist.");
			}
		}
	}
	
	function removeNewItems($id, $fields){
		$item_ids = array();
		foreach( $fields as $key => $value){
			$temp = $this->ci->input->post($value['new_field']);
			if( $temp ){
				$temp = array_keys($this->_parseOptionString($temp));
				$item_ids = array_merge( $item_ids, $temp);
			}
		}
		$this->model->deleteItems($item_ids);
	}
	
	function addItemOption($id, $fields){
		$params = array();
		foreach( $fields as $key => $value){
			$option = null;
			if( $this->ci->db->table_exists($value['table'])){ //$key
				$params[$key] = array();
				$this->_setupModel($value);
				$new_field = $value['new_field'];
				if( $new_field && $this->ci->input->post($new_field)){
					//echo "found $new_field: " . $this->input->post($new_field) . "<br />";
					$temp = $this->_parseOptionString($this->ci->input->post($new_field));
					$temp = array_values($temp);
					$option = array_pop($temp);
					//echo "option: $option <br />";
					$this->parent_model->fields[$new_field] = array(
													'edit'	=> array(
																'func'		=> array('Form_wrapper', 'form_input_wrapper'),
																'params'	=> array( array('value' => $this->ci->input->post($new_field),'id' => $new_field, 'type' => 'hidden')),
																'label'		=> false
															),
													'hidden'	=> true
												);
				}else{
					if( !empty($id) ){
						//echo "no new field, but have id: $id<br />";
						$option = $this->model->getSingleItemOption($id);
						//echo "option: $option<br />";
					}else{
						//echo "no id...<br />";
					}
				}
				/*$params[$key][] = $option;
				$params[$key][] = $id;*/
				$params[$key][] = $option[1];
				$params[$key][] = $option[0];
				unset($option);
			}else{
				show_error("ProcessSingleItem::addItemOptionParameters - table $key does not exist");
			}
		}
		//echo "params:";
		//echo_html_object($params);
		$this->parent_model->addEditParameters($params);
	}
}

?>